package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Device;
import system.Graphics;

import com.game.R;

public class Loading {
	static private int 						m_iPercent = 0;
	private final static int 				m_iNumberOfBullet = 20;
	static private float					m_fPercentPerIter = 100f/ m_iNumberOfBullet;
	
	static private int 						m_iPinID;
	static private TexSquare				m_pin;
	static private int						m_iBulletID;
	static private TexSquare				m_bullet;
	static private int						m_iBulletTransparentID;
	static private int						m_iBackgroundID;
	static private TexSquare				m_background;
	
	static private int						m_iBulletWidth;
	static private int						m_iPinWidth;

	static private GL10 					m_gl;
	static private int						m_iLeftMargin;
	
	static private Texture 					m_texture;
	static private float					m_fScreenW;
	static private float 					m_fScreenH;
	
	static boolean 							s_init = Initialize();
	
	
	public void Destroy() {
		// TODO Auto-generated method stub

	}
	
	public Loading(int numberOfPart){
		m_fPercentPerIter = 100f/numberOfPart;
	}
	
	static private boolean Initialize(){
		if (s_init)
			return true;
		m_gl = Graphics.GetGraphics();
		m_gl.glMatrixMode(GL10.GL_PROJECTION);
		m_gl.glPushMatrix();
		m_gl.glLoadIdentity();
		m_fScreenW = Device.GetWidth() >> 1;
		m_fScreenH = Device.GetHeight() >> 1;
		m_gl.glOrthof(-m_fScreenW, m_fScreenW, -m_fScreenH , m_fScreenH , -1.0f, 1.0f);

		m_texture = new Texture();
		// init background
		m_iBackgroundID = m_texture.initTexture(R.drawable.splashbackground);
		Device.DisableScaleFactor();
		m_background = new TexSquare(Device.GetWidth() , Device.GetHeight());
		Device.EnableScaleFactor();
		
		// init pin
		m_iPinID = m_texture.initTexture(R.drawable.pin_small);
		m_pin = new TexSquare(m_texture.getWidth(), m_texture.getHeight());
		m_iPinWidth = (int)(m_texture.getWidth()*Device.GetScaleFactorW());
		
		// init bullet
		m_iBulletID = m_texture.initTexture(R.drawable.bullet_small);
		m_bullet  = new TexSquare(m_texture.getWidth(), m_texture.getHeight());
		m_iBulletWidth = (int)(m_texture.getWidth()*Device.GetScaleFactorW());
		
		// init transparent bullet
		m_iBulletTransparentID = m_texture.initTexture(R.drawable.bullet_small_trans);
		
		m_iLeftMargin = (m_iPinWidth + m_iBulletWidth*m_iNumberOfBullet) >> 1;
		return true;
	}

	static public void Render() {
		
		m_gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		m_gl.glMatrixMode(GL10.GL_PROJECTION);
		m_gl.glPushMatrix();
		m_gl.glLoadIdentity();
		m_fScreenW = Device.GetWidth() >> 1;
		m_fScreenH = Device.GetHeight() >> 1;
		m_gl.glOrthof(-m_fScreenW, m_fScreenW, -m_fScreenH , m_fScreenH , -1.0f, 1.0f);

		m_gl.glMatrixMode(GL10.GL_MODELVIEW);
		m_gl.glPushMatrix();
		m_gl.glLoadIdentity();
		// background
		m_texture.loadTexture(m_iBackgroundID);
		m_background.Render();
		
		m_gl.glTranslatef(-m_iLeftMargin, -118, 0);
		// pin
		m_texture.loadTexture(m_iPinID);
		m_pin.Render();
		m_gl.glTranslatef(m_iPinWidth, 18, 0);
		
		// bullet
		int num = (int) (m_iPercent*m_iNumberOfBullet /100f);
		if (num > m_iNumberOfBullet)
			num = m_iNumberOfBullet;
		for (int i = 0; i < num; ++i){
			m_texture.loadTexture(m_iBulletID);
			m_bullet.Render();
			m_gl.glTranslatef(m_iBulletWidth, 0, 0);
		}
		for (int i = num; i < m_iNumberOfBullet; ++i){
			m_texture.loadTexture(m_iBulletTransparentID);
			m_bullet.Render();
			m_gl.glTranslatef(m_iBulletWidth, 0, 0);
		}
		m_gl.glPopMatrix();
		m_gl.glMatrixMode(GL10.GL_PROJECTION);
		m_gl.glPopMatrix();

	}
	
	static public void UpdateProgess() {
		m_iPercent += m_fPercentPerIter;
		if (m_iPercent > 100)
			m_iPercent = 100;
		Render();
	}
	
	static public void UpdateProgess(int addPercent) {
		m_iPercent += addPercent;
		if (m_iPercent > 100)
			m_iPercent = 100;
		Render();
	}
	
	static public void Reset() {
		m_iPercent = 0;
	}
	
	static public void SetNumberPart(int num){
		m_fPercentPerIter = 100f/num;
		Reset();
		Render();
	}

}
